package redispool

import (
	"time"

	"github.com/go-redis/redis"
	"github.com/sirupsen/logrus"
)

// GetKeyNotExist 判断key值是否存在,不存在为true，同时写入key，设置有效期
func GetKeyNotExist(client *redis.Client, key, val string, keySecond int) bool {

	ipValue, err := client.Get(key).Result()
	if err == redis.Nil {
		logrus.Println(key + " does not exists")
		err = client.Set(key, val, time.Duration(keySecond)*time.Second).Err()
		if err != nil {
			logrus.Errorln("Redis key set", err.Error())
			return false
		}
		return true
	} else if err != nil {
		logrus.Errorln(key, err.Error())
	} else {
		logrus.Println(key, ipValue)
		return false
	}
	return false

}

// GetKey 获取redis key value
func GetKey(client *redis.Client, key string) string {
	val, err := client.Get(key).Result()
	if err != nil {
		logrus.Errorln("Redis获取", key, "出错啦！", err)
	}
	logrus.Println(key, val)
	return val

}

// GetKeyIsExist 判断key是否存在，若存在为true
func GetKeyIsExist(client *redis.Client, key string) bool {

	val, err := client.Get(key).Result()
	if err == redis.Nil {
		logrus.Println(key, "does not exist")
		return false
	} else if err != nil {
		logrus.Errorln("Redis key Error：", key, err)
		return false
	} else {
		logrus.Println(key, val)
		return true
	}
}
